﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BIStudio.Framework.Utils
{
    /// <summary>
    /// 日期工具类
    /// </summary>
    public class DateTimeTool
    {
        /// <summary>
        /// DateTime --> string
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="fromart"></param>
        /// <returns></returns>
        public static string ConvertDataTimeToString(DateTime dt, string fromart = "yyyy-MM-dd HH:mm:ss.fff")
        {
            return dt.ToString(fromart);
        }
        /// <summary>
        /// DateTime --> long 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ConvertDataTimeToLong(DateTime dt)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan toNow = dt.Subtract(dtStart);
            long timeStamp = toNow.Ticks;
            //timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
            return timeStamp;
        }



        /// <summary>
        /// long --> DateTime
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static DateTime ConvertLongToDateTime(long d)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            //long lTime = long.Parse(d + "0000");
            long lTime = long.Parse(d.ToString());
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow);
            return dtResult;
        }
        /// <summary>
        /// 计算时间差
        /// </summary>
        /// <param name="starTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="action">取值类别,d=天，h=小时，m=分钟，s=秒，ad=总天数，ah=总小时，am=总分钟，as=总秒</param>
        /// <returns></returns>
        public static double DateTimeDiff(DateTime starTime, DateTime endTime, string action)
        {
            if (endTime < starTime)
                return 0;
            TimeSpan ts = endTime - starTime;
            switch (action)
            {
                case "d"://天
                    return ts.Days;
                case "h"://小时
                    return ts.Hours;
                case "m": //分钟
                    return ts.Minutes;
                case "s"://秒
                    return ts.Seconds;
                case "ad"://总天数
                    return ts.TotalDays;
                case "ah"://总小时
                    return ts.TotalHours;
                case "am"://总分钟
                    return ts.TotalMinutes;
                case "as": //总秒
                    return ts.TotalSeconds;
                default:
                    return 0;
            }
        }
        /// <summary>
        /// 获取相差天数
        /// </summary>
        /// <param name="minDate"></param>
        /// <param name="maxDate"></param>
        /// <returns></returns>
        public static int GetDifferDays(DateTime minDate, DateTime maxDate)
        {
            int days = (maxDate - minDate).Days;//天数差
            if (days <= 0)
            {
                days = 0;
            }
            return days;
        }
        /// <summary>
        /// 时间格式化
        /// </summary>
        /// <param name="value"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string CheckDateTime(object value, string format)
        {
            if (value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                return DateTime.Parse(value.ToString()).ToString(format);
            }
            else
            {
                return "";
            }
        }

        /// <summary>  
        /// 获取指定日期所在周的第一天，星期天为第一天  
        /// </summary>  
        /// <param name="dateTime"></param>  
        /// <returns></returns>  
        public static DateTime GetDateTimeWeekFirstDaySun(DateTime dateTime)
        {
            DateTime firstWeekDay = DateTime.Now;

            try
            {
                //得到是星期几，然后从当前日期减去相应天数   
                int weeknow = Convert.ToInt32(dateTime.DayOfWeek);

                int daydiff = (-1) * weeknow;

                firstWeekDay = dateTime.AddDays(daydiff);
            }
            catch { }

            return firstWeekDay;
        }

        /// <summary>  
        /// 获取指定日期所在周的第一天，星期一为第一天  
        /// </summary>  
        /// <param name="dateTime"></param>  
        /// <returns></returns>  
        public static DateTime GetDateTimeWeekFirstDayMon(DateTime dateTime)
        {
            DateTime firstWeekDay = DateTime.Now;

            try
            {
                int weeknow = Convert.ToInt32(dateTime.DayOfWeek);

                //星期一为第一天，weeknow等于0时，要向前推6天。     
                weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1));

                int daydiff = (-1) * weeknow;

                firstWeekDay = dateTime.AddDays(daydiff);
            }
            catch { }

            return firstWeekDay;
        }

        /// <summary>  
        /// 获取指定日期所在周的最后一天，星期六为最后一天  
        /// </summary>  
        /// <param name="dateTime"></param>  
        /// <returns></returns>  
        public static DateTime GetDateTimeWeekLastDaySat(DateTime dateTime)
        {
            DateTime lastWeekDay = DateTime.Now;

            try
            {
                int weeknow = Convert.ToInt32(dateTime.DayOfWeek);

                int daydiff = (7 - weeknow) - 1;

                lastWeekDay = dateTime.AddDays(daydiff);

            }
            catch { }

            return lastWeekDay;
        }

        /// <summary>  
        /// 获取指定日期所在周的最后一天，星期天为最后一天  
        /// </summary>  
        /// <param name="dateTime"></param>  
        /// <returns></returns>  

        public static DateTime GetDateTimeWeekLastDaySun(DateTime dateTime)
        {
            DateTime lastWeekDay = DateTime.Now;

            try
            {
                int weeknow = Convert.ToInt32(dateTime.DayOfWeek);

                weeknow = (weeknow == 0 ? 7 : weeknow);

                int daydiff = (7 - weeknow);

                lastWeekDay = dateTime.AddDays(daydiff);
            }
            catch { }

            return lastWeekDay;
        }

        /// <summary>  
        /// 获取指定日期的月份第一天  
        /// </summary>  
        /// <param name="dateTime"></param>  
        /// <returns></returns>  
        public static DateTime GetDateTimeMonthFirstDay(DateTime dateTime)
        {
            if (dateTime == null)
            {
                dateTime = DateTime.Now;
            }

            return new DateTime(dateTime.Year, dateTime.Month, 1);
        }

        /// <summary>  
        /// 获取指定月份最后一天  
        /// </summary>  
        /// <param name="dateTime"></param>  
        /// <returns></returns>  
        public static DateTime GetDateTimeMonthLastDay(DateTime dateTime)
        {
            int day = DateTime.DaysInMonth(dateTime.Year, dateTime.Month);

            return new DateTime(dateTime.Year, dateTime.Month, day);
        }

        /// <summary>
        /// 获取星期
        /// </summary>
        /// <param name="SaleDate"></param>
        /// <returns></returns>
        public static string GetWeek(object SaleDate)
        {
            string week = string.Empty;
            string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
            DateTime? d = ALConvert.ToDateTime(SaleDate);
            if (d.HasValue)
            {
                week = Day[Convert.ToInt32(d.Value.DayOfWeek)].ToString();
            }
            return week;
        }

    }
}
